﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;
using System.Threading;
using System.Windows.Threading;
using Windows.Devices.Geolocation;


namespace Puddy
{
    public class Gps
    {
       
        private static GeoCoordinateWatcher gps = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
        public static int MovementThresHold
        {
            get;
            set;
        }

        public static GeoPosition<GeoCoordinate> CurrentPosition
        {
            get
            {
                return gps.Position;
            }
        }
        static Gps()
        {
            MovementThresHold = 20;
            gps.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gps_PositionChanged);
            gps.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(gps_StatusChanged);

            PositionChanged += Gps_PositionChanged;
            StatusChanged += Gps_StatusChanged;
        }

        static void Gps_PositionChanged(GeoCoordinate obj)
        {

        }

        static void Gps_StatusChanged(GeoPositionStatus obj)
        {

        }

        public static event Action<GeoCoordinate> PositionChanged;
        public static event Action<GeoPositionStatus> StatusChanged;
        public static event Action OnTimeOut;

        static void gps_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            StatusChanged(e.Status);
        }

        static void gps_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (IsTimeOut == false)
            {
                IsTimeOut = true;
                PositionChanged(e.Position.Location);
            }

        }

        public static void CallPositionChange(double lat, double lng)
        {
            PositionChanged(new GeoCoordinate(lat, lng));
        }


        public static bool IsTimeOut = false;
        private static DispatcherTimer timer = new DispatcherTimer();
        public static void Start()
        {
            IsTimeOut = false;
            gps = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            gps.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gps_PositionChanged);
            gps.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(gps_StatusChanged);
            gps.MovementThreshold = MovementThresHold;
            gps.Start();
            //DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += timer_Tick;
            timer.Interval = new TimeSpan(0, 0, 20);
            timer.Start();


        }

        static void timer_Tick(object sender, EventArgs e)
        {
            //if (AppViewModel.Instance.SettingVM.Language == "Vi")
            //{
            //    MainPage.Instance.Toast(LangVi.errorGetGPS);
            //}
            //else
            //{
            //    MainPage.Instance.Toast(LangEn.errorGetGPS);
            //}
            //MainPage.Instance.HideWaiting();
            (sender as DispatcherTimer).Stop();
            if (IsTimeOut == false)
            {
                OnTimeOut();
                IsTimeOut = true;
            }
        }



        public static void Stop()
        {
            gps.Stop();
        } 
       
    }
}
